package com.zhou.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.commonutils.R;
import com.zhou.entity.Exam;
import com.zhou.entity.Notice;
import com.zhou.entity.Subject;
import com.zhou.entity.SubjectType;
import com.zhou.entity.vo.*;
import com.zhou.service.*;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "三个角色公共的相关接口")
@RequestMapping(value = "/public")
public class PublicController {

    private final NoticeService noticeService;

    private final ExamService examService;

    private final QuestionBankService questionBankService;

    @Autowired
    private SubjectTypeService subjectTypeService;

    @Autowired
    private SubjectService subjectService;


    @PostMapping("/getExamInfo")
    @ApiOperation("根据信息查询考试的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examQueryVo", value = "考试信息查询vo对象", required = true, dataType = "examQueryVo", paramType = "body")
    })
    public CommonResult<PageResponse<Exam>> getExamInfo(@RequestBody ExamQueryVo examQueryVo) {
        return CommonResult.<PageResponse<Exam>>builder()
                .data(examService.getExamPage(examQueryVo))
                .build();
    }

    @GetMapping("/getExamInfoById")
    @ApiOperation("根据考试id查询考试的信息和题目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试id", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<AddExamByQuestionVo> getExamInfoById(@RequestParam Integer examId) {
        return CommonResult.<AddExamByQuestionVo>builder()
                .data(examService.getExamInfoById(examId))
                .build();
    }

    @GetMapping("/allExamInfo")
    @ApiOperation("查询考试所有信息")
    public CommonResult<List<Exam>> allExamInfo() {
        return CommonResult.<List<Exam>>builder()
                .data(examService.list(null))
                .build();
    }

    @GetMapping("/getBankHaveQuestionSumByType")
    @ApiOperation("获取题库中所有题目类型的数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subject_id", value = "科目Id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "bankName", value = "题库名称", dataType = "string", paramType = "query"),

            @ApiImplicitParam(name = "pageNo", value = "页面数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int", paramType = "query"),
    })
    public CommonResult<PageResponse<BankHaveQuestionSum>> getBankHaveQuestionSumByType(@RequestParam(required = false) Integer subject_id,String bankName,
                                                                                        Integer pageNo, Integer pageSize) {


        System.out.println(1111111+"---"+bankName+","+subject_id+","+pageNo+","+pageSize);
        return CommonResult.<PageResponse<BankHaveQuestionSum>>builder()
                .data(questionBankService.getBankHaveQuestionSumByType(subject_id,bankName, pageNo, pageSize))
                .build();
    }

    @GetMapping("/getQuestionByBankIdAndType")
    @ApiOperation("根据题库id和题目类型获取题目信息 type(1单选 2多选 3判断)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "题库id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "题目类型", required = true, dataType = "int", paramType = "query"),
    })
    public CommonResult<List<QuestionVo>> getQuestionByBankIdAndType(Integer bankId, Integer type) {
        return CommonResult.<List<QuestionVo>>builder()
                .data(questionBankService.getQuestionByBankIdAndType(bankId, type))
                .build();
    }

    @GetMapping("/getQuestionByBank")
    @ApiOperation("根据题库获取所有的题目信息(单选,多选,判断题)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "题库id", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<List<QuestionVo>> getQuestionByBank(Integer bankId) {
        return CommonResult.<List<QuestionVo>>builder()
                .data(questionBankService.getQuestionsByBankId(bankId))
                .build();
    }

    @GetMapping("/getCurrentNewNotice")
    @ApiOperation("获取当前系统最新的公告")
    public CommonResult<String> getCurrentNewNotice() {
        return CommonResult.<String>builder()
                .data(noticeService.getCurrentNotice())
                .build();
    }
    @GetMapping("getSubject")
    @ApiOperation("获取全部科目类型")
    public R list(){
        List<SubjectType> list= subjectTypeService.list(null);
        return R.ok().data("items",list);
    }

    @GetMapping("getKemu")
    @ApiOperation("获取全部科目")
    public R Kemulist(){
        List<Subject> list= subjectService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "分页科目类型列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit){
        Page<SubjectType> pageParam=new Page<SubjectType>(page,limit);
        subjectTypeService.page(pageParam,null);
        List<SubjectType> records=pageParam.getRecords();
        long total=pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }
    @ApiOperation(value = "分页科目列表")
    @GetMapping("/kemu/{page}/{limit}")
    public R KemuPageList(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit){
        Page<Subject> pageParam=new Page<Subject>(page,limit);
        subjectService.page(pageParam,null);
        List<Subject> records=pageParam.getRecords();
        long total=pageParam.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增科目类型")
    @PostMapping("addSubject")
    public R save(
            @ApiParam(name = "subjectType",value = "科目对象",required = true)
            @RequestBody SubjectType subjectType){
        boolean flag= subjectTypeService.save(subjectType);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "新增科目")
    @PostMapping("/kemu/addSubject")
    public R KemuSave(
            @ApiParam(name = "subject",value = "科目对象",required = true)
            @RequestBody Subject subject){
        boolean flag= subjectService.save(subject);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询科目类型信息")
    @GetMapping("getSubject/{id}")
    public R getById(
            @ApiParam(name = "id",value = "科目ID",required = true)
            @PathVariable String id){
        SubjectType subjectType = subjectTypeService.getById(id);
        return R.ok().data("item", subjectType);
    }

    @ApiOperation(value = "根据ID查询科目信息")
    @GetMapping("/kemu/getSubject/{id}")
    public R kemuGetById(
            @ApiParam(name = "id",value = "科目ID",required = true)
            @PathVariable String id){
        Subject subject = subjectService.getById(id);
        return R.ok().data("item", subject);
    }



    @ApiOperation(value = "修改科目类型信息")
    @PostMapping("updateSubject")
    public R updateSubject( @ApiParam(name = "subjectType",value = "科目对象",required = true)
                            @RequestBody SubjectType subjectType){
        boolean flag= subjectTypeService.updateById(subjectType);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "修改科目信息")
    @PostMapping("/kemu/updateSubject")
    public R kemuUpdateSubject( @ApiParam(name = "subject",value = "科目对象",required = true)
                            @RequestBody Subject subject){
        boolean flag= subjectService.updateById(subject);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation(value = "分页科目列表-条件查询")
    @PostMapping("query/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "subjectQuery", value = "查询对象", required = false)
            @RequestBody(required = false) SubjectQuery subjectQuery){

        Page<Subject> pageParam = new Page<Subject>(page, limit);
        subjectService.pageQuery(pageParam, subjectQuery);
        List<Subject> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }
    @GetMapping("/subject/{subjectTypeId}")
    @ResponseBody
    public R countSubject(){
        List<Subject> count=subjectService.countSubject();

        return R.ok().data("Subject",count);
    }
    @GetMapping("/getAllNotice")
    @ApiOperation("获取系统发布的所有公告(分页 条件查询  二合一接口)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeContent", value = "搜索公告内容", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "查询结果分页当前页面", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "查询结果的页面条数大小", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<PageResponse<Notice>> getAllNotice(@RequestParam(required = false, name = "noticeContent") String content,
                                                           Integer pageNo, Integer pageSize) {
        return CommonResult.<PageResponse<Notice>>builder()
                .data(noticeService.getAllNotices(content, pageNo, pageSize))
                .build();
    }

    @PostMapping("/publishNotice")
    @ApiOperation("发布新公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "通知实体对象", required = true, dataType = "notice", paramType = "body")
    })
    public CommonResult<Void> publishNotice(@RequestBody Notice notice) {
        noticeService.publishNotice(notice);
        return CommonResult.<Void>builder()
                .build();
    }

    @GetMapping("/deleteNotice")
    @ApiOperation("批量删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeIds", value = "系统公告id", required = true, dataType = "string", paramType = "query")
    })
    public CommonResult<Void> deleteNotice(@RequestParam(name = "ids") String noticeIds) {
        noticeService.deleteNoticeByIds(noticeIds);
        return CommonResult.<Void>builder().build();
    }

    @PostMapping("/updateNotice")
    @ApiOperation("更新公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "通知实体对象", required = true, dataType = "notice", paramType = "body")
    })
    public CommonResult<Void> updateNotice(@RequestBody Notice notice) {
        noticeService.updateNotice(notice);
        return CommonResult.<Void>builder().build();
    }
}
